<?php
/*

*    代小程序实现业务

*/

namespace app\home\model;


use think\Db;

use think\Cache;

class Miniprogram extends Base

{

    private $thirdAppId;        //开放平台appid

    private $encodingAesKey;    //开放平台encodingAesKey

    private $thirdToken;        //开放平台token

    private $thirdAccessToken;  //开放平台access_token



    public $authorizer_appid;

    private  $authorizer_access_token;

    private  $authorizer_refresh_token;

    public $error;//1错误

    public $msg;



    public function __construct($appid)

    {
        $weixin_account = Db::name('weixin_account')->where(['type' => 1])->field('token,encodingAesKey,appId,component_access_token,token_expires')->find();

        if ($weixin_account) {

            $this->thirdAppId = $weixin_account['appId'];

            $this->encodingAesKey = $weixin_account['encodingAesKey'];

            $this->thirdToken = $weixin_account['token'];

            $this->thirdAccessToken = $this->updateAccessToken();



            $miniprogram = Db::name('wxminiprograms')->where('authorizer_appid',$appid)

                ->field('authorizer_access_token,authorizer_refresh_token,authorizer_expires')->find();

            if($miniprogram){

                $this->authorizer_appid = $appid;

                if(time() > $miniprogram['authorizer_expires']){

                    $miniapp = $this->update_authorizer_access_token($appid,$miniprogram['authorizer_refresh_token']);

                    if($miniapp) {

                        $this->authorizer_access_token = $miniapp->authorizer_access_token;

                        $this->authorizer_refresh_token = $miniapp->authorizer_refresh_token;

                    } else {

                        $this->errorLog("更新小程序access_token失败,appid:".$this->authorizer_appid,'');

                        exit;

                    }

                } else {

                    $this->authorizer_access_token = $miniprogram['authorizer_access_token'];

                    $this->authorizer_refresh_token = $miniprogram['authorizer_refresh_token'];

                }



            } else {
                $this->errorLog("小程序不存在,appid:".$appid,'');
                $this->error = 1;
                $this->msg = "小程序不存在,appid:".$appid;

            }

        } else {

            $this->errorLog("请增加微信第三方公众号平台账户信息",'');

            exit;

        }

    }

    /* 更新component_access_token

    * @params string $component_verify_ticket

    * */

    private function updateAccessToken()

    {

        $weixin_account = Db::name('weixin_account')->where(['type'=>1])->field('id,appId,appSecret,component_access_token,token_expires,component_verify_ticket')->find();

        if($weixin_account['token_expires'] <= time() ) {

            $apiUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';

            $data = '{"component_appid":"'.$weixin_account['appId'].'" ,"component_appsecret": "'.$weixin_account['appSecret'].'","component_verify_ticket": "'.$weixin_account['component_verify_ticket'].'"}';

            $json = json_decode(https_post($apiUrl,$data));

            if(isset($json->component_access_token)) {

                Db::name('weixin_account')->where(['id'=>$weixin_account['id']])->update(['component_access_token'=>$json->component_access_token,'token_expires'=>time()+7200]);

                return $json->component_access_token;
            }
        }
        return $weixin_account['component_access_token'];

    }

    /*

     * 设置小程序服务器地址，无需加https前缀，但域名必须可以通过https访问

     * @params string / array $domains : 域名地址。只接收一维数组。

     * */

    public  function setServerDomain($domain = 'test.moh.cc')

    {

        $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=".$this->authorizer_access_token;

        if(is_array($domain)) {

            $https = ''; $wss = '';

            foreach ($domain as $key => $value) {

                $https .= '"https://'.$value.'",';

                $wss .= '"wss://'.$value.'",';

            }

            $https = rtrim($https,',');

            $wss = rtrim($wss,',');

            $data = '{

                "action":"add",

                "requestdomain":['.$https.'],

                "wsrequestdomain":['.$wss.'],

                "uploaddomain":['.$https.'],

                "downloaddomain":['.$https.']

            }';

        } else {

            $data = '{

                "action":"add",

                "requestdomain":"https://'.$domain.'",

                "wsrequestdomain":"wss://'.$domain.'",

                "uploaddomain":"https://'.$domain.'",

                "downloaddomain":"https://'.$domain.'"

            }';

        }

        $ret = json_decode(https_post($url,$data));

        if($ret->errcode == 0) {

            return true;

        } else {

            $this->errorLog("设置小程序服务器地址失败,appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

     * 设置小程序业务域名，无需加https前缀，但域名必须可以通过https访问

     * @params string / array $domains : 域名地址。只接收一维数组。

     * */

    public function setBusinessDomain($domain = 'test.moh.cc')

    {

        $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=".$this->authorizer_access_token;

        if(is_array($domain)) {

            $https = '';

            foreach ($domain as $key => $value) {

                $https .= '"https://'.$value.'",';

            }

            $https = rtrim($https,',');

            $data = '{

                "action":"add",

                "webviewdomain":['.$https.']

            }';

        } else {

            $data = '{

                "action":"add",

                "webviewdomain":"https://'.$domain.'"

            }';

        }



        $ret = json_decode(https_post($url,$data));

        if($ret->errcode == 0) {

            return true;

        } else {

            $this->errorLog("设置小程序业务域名失败,appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

     * 成员管理，绑定小程序体验者

     * @params string $wechatid : 体验者的微信号

     * */

    public function bindMember($wechatid)

    {

        $url = "https://api.weixin.qq.com/wxa/bind_tester?access_token=".$this->authorizer_access_token;

        $data = '{"wechatid":"'.$wechatid.'"}';

        $ret = json_decode(https_post($url,$data));

        if($ret->errcode == 0) {

            return true;

        } else {

            $this->errorLog("绑定小程序体验者操作失败,appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

     * 成员管理，解绑定小程序体验者

     * @params string $wechatid : 体验者的微信号

     * */

    public function unBindMember($wechatid)

    {

        $url = "https://api.weixin.qq.com/wxa/unbind_tester?access_token=".$this->authorizer_access_token;

        $data = '{"wechatid":"'.$wechatid.'"}';

        $ret = json_decode(https_post($url,$data));

        if($ret->errcode == 0) {

            return true;

        } else {

            $this->errorLog("解绑定小程序体验者操作失败,appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

    * 成员管理，获取小程序体验者列表

    * */

    public function listMember()

    {

        $url = "https://api.weixin.qq.com/wxa/memberauth?access_token=".$this->authorizer_access_token;

        $data = '{"action":"get_experiencer"}';

        $ret = json_decode(https_post($url,$data),true);

        if($ret['errcode'] == 0) {

            return $ret['members'];

        } else {

            $this->errorLog("获取小程序体验者列表操作失败,appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

     * 为授权的小程序帐号上传小程序代码

     * @params int $template_id : 模板ID

     * @params json $ext_json : 小程序配置文件，json格式

     * @params string $user_version : 代码版本号

     * @params string $source_miniprogram : 代码描述

     * */

    public function uploadCode($template_id = 1, $user_version=null , $source_miniprogram=null)

    {
        $template = $this->get_template_list();
        $count = count($template)-1;
        $user_version = empty($user_version)?$template[$count]['user_version']:$user_version;
        $source_miniprogram = empty($user_desc)?$template[$count]['user_desc']:$source_miniprogram;

        $ext_json = json_encode('{"extEnable": true,"extAppid": "'.$this->authorizer_appid.'","ext":{"appid": "'.$this->authorizer_appid.'"}}');

        $url = "https://api.weixin.qq.com/wxa/commit?access_token=".$this->authorizer_access_token;

        $data = '{"template_id":"'.$count.'","ext_json":'.$ext_json.',"user_version":"'.$user_version.'","user_desc":"'.$source_miniprogram.'"}';

        $ret = json_decode(https_post($url,$data));

        if($ret->errcode == 0) {

            return true;

        } else {

            $this->errorLog("为授权的小程序帐号上传小程序代码操作失败,appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

     * 获取体验小程序的体验二维码

     * @params string $path :   指定体验版二维码跳转到某个具体页面

     * */

    public function getExpVersion($path = '')

    {

        if($path){

            $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=".$this->authorizer_access_token."&path=".urlencode($path);

        } else {

            $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=".$this->authorizer_access_token;

        }

        $ret = json_decode(https_get($url));

        if(!empty($ret->errcode)) {

            $this->errorLog("获取体验小程序的体验二维码操作失败,appid:".$this->authorizer_appid,$ret);

            return false;

        } else {

            return $url;

        }

    }

    /*

     * 提交审核

     * @params string $tag : 小程序标签，多个标签以空格分开

     * @params strint $title : 小程序页面标题，长度不超过32

     * */

    public function submitReview($tag = "电子商务 智能建站 CRM客户管理 企业管理" ,$title = "侠客人工智能名片")

    {
        $template = $this->get_template_list();
        $count = count($template)-1;
        $version = $template[$count]['user_version'];//版本号

        $first_class = '';$second_class = '';$first_id = 0;$second_id = 0;

        $address = "pages/index/index";

        $category = $this->getCategory();

        if(!empty($category)) {

            $first_class = $category[0]->first_class ? $category[0]->first_class : '' ;

            $second_class = $category[0]->second_class ? $category[0]->second_class : '';

            $first_id = $category[0]->first_id ? $category[0]->first_id : 0;

            $second_id = $category[0]->second_id ? $category[0]->second_id : 0;

        }

        $getpage = $this->getPage();

        if(!empty($getpage) && isset($getpage[0])) {

            $address = $getpage[0];

        }

        $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=".$this->authorizer_access_token;

        $data = '{

                "item_list":[{

                    "address":"'.$address.'",

                    "tag":"'.$tag.'",

                    "title":"'.$title.'",

                    "first_class":"'.$first_class.'",

                    "second_class":"'.$second_class.'",

                    "first_id":"'.$first_id.'",

                    "second_id":"'.$second_id.'"

                }]

            }';

        $ret = json_decode(https_post($url,$data));

        if($ret->errcode == 0) {

            Db::name('wxminiprogram_audit')->insert([

                'appid'=>$this->authorizer_appid,

                'version'=>$version,//版本号

                'auditid'=>$ret->auditid,

                'create_time'=>date('Y-m-d H:i:s')

            ]);
            $this->error = 0;
            $this->msg = '审核已提交，请等待结果';

            return true;

        } else {

            $this->errorLog("小程序提交审核操作失败，appid:".$this->authorizer_appid,$ret);

            $this->error = 50001;
            $this->msg = '审核提交失败，已在审核中，请等待结果';
            return false;

        }

    }

    /*

     * 小程序审核撤回

     * 单个帐号每天审核撤回次数最多不超过1次，一个月不超过10次。

     * */

    public function unDoCodeAudit()

    {

        $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=".$this->authorizer_access_token;

        $ret = json_decode(https_get($url));

        if($ret->errcode == 0) {

            return true;

        } else {

            $this->errorLog("小程序审核撤回操作失败，appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

     * 查询指定版本的审核状态

     * @params string $auditid : 提交审核时获得的审核id

     * */

    public function getAuditStatus($auditid)

    {

        $url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token=".$this->authorizer_access_token;

        $data = '{"auditid":"'.$auditid.'"}';

        $ret = json_decode(https_post($url,$data));

        if($ret->errcode == 0) {

            $reason = $ret->reason ? $ret->reason : '';

            Db::name('wxminiprogram_audit')->where(['appid'=>$this->authorizer_appid,'auditid'=>$auditid])->update([

                'status'=>$ret->status,

                'reason'=>$reason

            ]);

            return true;

        } else {

            $this->errorLog("查询指定版本的审核状态操作失败，appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

     * 查询最新一次提交的审核状态

     * */

    public function getLastAudit()

    {

        $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=".$this->authorizer_access_token;

        $ret = json_decode(https_get($url));
        if($ret->errcode == 0) {

            $reason = !empty($ret->reason) ? $ret->reason : '';

            Db::name('wxminiprogram_audit')->where(['appid'=>$this->authorizer_appid,'auditid'=>$ret->auditid])->update([

                'status'=>$ret->status,

                'reason'=>$reason

            ]);

            return $ret->status;

        } else {

            $this->errorLog("查询最新一次提交的审核状态操作失败，appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

     * 发布已通过审核的小程序

     * */

    public function release()

    {

        $url = "https://api.weixin.qq.com/wxa/release?access_token=".$this->authorizer_access_token;

        $data = '{}';

        $ret = json_decode(https_post($url,$data));

        if($ret->errcode == 0) {

            $reason = !empty($ret->reason) ? $ret->reason : '';

            //Db::name('wxminiprogram_audit')->where(['appid'=>$this->authorizer_appid,'auditid'=>$ret->auditid])->update([

               // 'status'=>$ret->status,

              //  'reason'=>$reason

          //  ]);
            $app = model('WxminiprogramAudit')->where('appid',$this->authorizer_appid)->order('id','desc')->find();
            $app->update(['status'=>$ret->errcode,'reason'=>$reason]);
            $this->errorLog("小程序自动发布，appid:".$this->authorizer_appid,$ret);


            return true;

        } else {

            $this->errorLog("发布已通过审核的小程序操作失败，appid:".$this->authorizer_appid,$ret);

            return $ret->errcode;

        }

    }

    /*

     * 获取授权小程序帐号的可选类目

     * */

    private function getCategory()

    {

        $url = "https://api.weixin.qq.com/wxa/get_category?access_token=".$this->authorizer_access_token;

        $ret = json_decode(https_get($url));

        if($ret->errcode == 0) {

            return $ret->category_list;

        } else {

            $this->errorLog("获取授权小程序帐号的可选类目操作失败，appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

     * 获取小程序的第三方提交代码的页面配置

     * */

    private function getPage()

    {

        $url = "https://api.weixin.qq.com/wxa/get_page?access_token=".$this->authorizer_access_token;

        $ret = json_decode(https_get($url));

        if($ret->errcode == 0) {

            return $ret->page_list;

        } else {

            $this->errorLog("获取小程序的第三方提交代码的页面配置失败，appid:".$this->authorizer_appid,$ret);

            return false;

        }

    }

    /*

    * 更新授权小程序的authorizer_access_token

    * @params string $appid : 小程序appid

    * @params string $refresh_token : 小程序authorizer_refresh_token

    * */

    private function update_authorizer_access_token($appid,$refresh_token)

    {

        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=' . $this->thirdAccessToken;

        $data = '{"component_appid":"' . $this->thirdAppId . '","authorizer_appid":"' . $appid . '","authorizer_refresh_token":"' . $refresh_token . '"}';

        $ret = json_decode(https_post($url, $data));

        if (isset($ret->authorizer_access_token)) {

            Db::name('wxminiprograms')->where(['authorizer_appid' => $appid])->update(['authorizer_access_token' => $ret->authorizer_access_token, 'authorizer_expires' => (time() + 7200), 'authorizer_refresh_token' => $ret->authorizer_refresh_token]);

            return $ret;

        } else {

            $this->errorLog("更新授权小程序的authorizer_access_token操作失败,appid:".$appid,$ret);

            return null;

        }

    }

    /*
     * 小程序消息模板添加
     * @params string $id 模板标题ID
     * @params string $keyword_id_list 小程序模板关键词
     * */
    public function templateAdd($id,$keyword_id_list)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=$this->authorizer_access_token";
        $data = '{
            "id":"'.$id.'",
            "keyword_id_list":'.$keyword_id_list.'
            }';

        $ret =  json_decode(https_post($url,$data));

        if(isset($ret->template_id)){
            if($id=='AT0009'){
                model('Wxminiprograms')->where('authorizer_appid',$this->authorizer_appid)->update(['payment_template_id'=>$ret->template_id]);
            }else if($id=='AT0891'){
                model('Wxminiprograms')->where('authorizer_appid',$this->authorizer_appid)->update(['msg_template_id'=>$ret->template_id]);

            }
            return $ret;
        }else{
            $this->errorLog("小程序的消息模板添加失败,模板ID:$id,appid:".$this->authorizer_appid,$ret);
            return null;
        }
    }

    /*
     * 获取代码模版库中的所有小程序代码模版
     * @return $array template_list 所有版本
     * */
    public function get_template_list()
    {
        $url = "https://api.weixin.qq.com/wxa/gettemplatelist?access_token=$this->thirdAccessToken";
        return json_decode(https_post($url,''),true)['template_list'];
    }

    /*
     * 返回app token
     * */
    public function app_access_token()
    {
        return $this->authorizer_access_token;
    }


    private function errorLog($msg,$ret)

    {
        file_put_contents('../runtime/error/miniprogram.log', "[" . date('Y-m-d H:i:s') . "] ".$msg."," .json_encode($ret).PHP_EOL, FILE_APPEND);

    }

}